from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
import json

from userServer.models import UserDetail

"""详细信息api"""
@csrf_exempt
def get_user_detail(request):
    """获取详细信息，用json返回，先检查requst中有没有session"""
    if request.session.get('user_id') is None:
        return HttpResponse("请先登录", status=401)
    if request.method == 'Post':
        # 获取用户id
        user_id = request.session.get('user_id')
        # 查询数据库
        try:
            # 查询数据库
            user_detail = UserDetail.objects.get(userId=user_id)
            # 返回json
            return HttpResponse(json.dumps({
                'id':user_detail.id,
                'nickname': user_detail.nickname,
                'introduce': user_detail.introduce,
                'phone': user_detail.phone,
                'job': user_detail.job,
                'education': user_detail.education,
                'create_time': user_detail.create_time.strftime("%Y-%m-%d %H:%M:%S"),
                'update_time': user_detail.update_time.strftime("%Y-%m-%d %H:%M:%S"),
                'userId': user_detail.userId.id,
                'address': user_detail.address,
                'age': user_detail.age,
                'sex': user_detail.sex,
                'company': user_detail.company,
                'birsthday': user_detail.birsthday.strftime("%Y-%m-%d %H:%M:%S"),
            }))
        except UserDetail.DoesNotExist:
            return HttpResponse("用户不存在", status=404)
@csrf_exempt
def update_user_detail(request):
    """更新个人信息"""
    if request.session.get('user_id') is None:
        return HttpResponse("请先登录", status=401)
    if request.method == 'POST':
        user_id = request.session.get('user_id')
        try:
            data = json.loads(request.body)
            user_detail = {
                    'nickname': data['nickname'],
                    'introduce': data['introduce'],
                    'phone': data['phone'],
                    'job': data['job'],
                    'education': data['education'],
                    'address': data['address'],
                    'age': data['age'],
                    'sex': data['sex'],
                    'company': data['company'],
                    'birsthday': data['birsthday'],
                    'update_time': data['update_time'],
                }

            UserDetail.objects.filter(userId=user_id).update(**user_detail)
            return HttpResponse("更新成功", status=200)
        except UserDetail.DoesNotExist:
            return HttpResponse("用户不存在", status=404)
        except Exception as e:
            return HttpResponse("更新失败", status=500)
    else:
        return HttpResponse("更新失败", status=500)
"""创建信息"""
@csrf_exempt
def create_user_detail(request):
    if request.session.get('user_id') is None:
        return HttpResponse("请先登录", status=401)
    if request.method == 'POST':
        user_id = request.session.get('user_id')
        try:
            data = json.loads(request.body)
            user_detail = {
                    'nickname': data['nickname'],
                    'introduce': data['introduce'],
                    'phone': data['phone'],
                    'job': data['job'],
                    'education': data['education'],
                    'address': data['address'],
                    'age': data['age'],
                    'sex': data['sex'],
                    'company': data['company'],
                    'birsthday': data['birsthday'],
                    'create_time': data['create_time'],
                    'update_time': data['update_time'],
                    'userId': user_id,
            }
            UserDetail.objects.create(**user_detail)
            return HttpResponse("创建成功", status=200)
        except Exception as e:
            return HttpResponse("创建失败", status=500)
        except UserDetail.DoesNotExist:
            return HttpResponse("用户不存在", status=404)
        except Exception as e:
            return HttpResponse("创建失败", status=500)
    else:
        return HttpResponse("bye--", status=500)